package indexer;

import interfacemanagement.SetUpInitialValues;
import java.io.File;
import java.io.IOException;
import searcher.DocumentSearcher;
import domain.Document;

public class FileManager {
	private enum FileType {
		TXT, JAVA, LOG, HTML,XML, NOT_SUPPORTED
	}

	private String message;
	private FileType fileType;
	private IndexerFileReader textReader;
	private FileBuffer buffer;
	private FileBufferPopulator bufferPopulator;
	private Document document;
	private SetUpInitialValues initialValues;
	private Integer numberOfDocuments;

	public FileManager() throws ClassNotFoundException, IOException {
		buffer = new FileBuffer();
		bufferPopulator = new FileBufferPopulator();
		initialValues = new SetUpInitialValues();
		setUpInitialValues();
	}

	public FileManager(Document document) throws ClassNotFoundException, IOException {
		buffer = new FileBuffer();
		bufferPopulator = new FileBufferPopulator();
		this.document = document;
		setUpInitialValues();
	}

	public String getMessage() {
		return message;
	}

	public boolean processFile(File file) throws ClassNotFoundException, IOException {
		if (file == null)
			return false;
		setReaderType(file);
		if (fileType == FileType.NOT_SUPPORTED)
			return false;
		initializeDocument(file);
		buffer.buildVocabulary(document, textReader.processFile(file));
		if (buffer.getVocabulary() != null && buffer.getVocabulary().size() > 0) {
			document.setTotalOfDocuments(this.numberOfDocuments + 1);
			bufferPopulator.populateVocabulary(buffer.getVocabulary(), document);
		}
		// increment the amount of documents already processed
		this.numberOfDocuments++;

		// actualizar el vocabulario del buscador
		DocumentSearcher.setVocabularies(bufferPopulator.getDocVocabulary());
		return true;
	}

	private void setReaderType(File file) {
		setTypeFile(file);
		switch (fileType) {
		case TXT:
		case JAVA:
		case LOG:
			textReader = new PlainTextReader();
			break;
		case HTML:
		case XML:
			textReader = new HTMLReader();
			break;
		case NOT_SUPPORTED:
			break;
		}
	}

	private void setTypeFile(File file) {
		String extension = file.getName().substring(file.getName().lastIndexOf('.') + 1);
		if (extension.equalsIgnoreCase("txt"))
			fileType = FileType.TXT;
		else if (extension.equalsIgnoreCase("java"))
			fileType = FileType.JAVA;
		else if (extension.equalsIgnoreCase("log"))
			fileType = FileType.LOG;
		else if (extension.equalsIgnoreCase("htm") || (extension.equalsIgnoreCase("html")))
			fileType = FileType.HTML;
		else if (extension.equalsIgnoreCase("xml"))
			fileType = FileType.XML;
		else
			fileType = FileType.NOT_SUPPORTED;
	}

	private void initializeDocument(File file) {
		this.document = new Document();
		document.setName(file.getName());
		document.setPath(file.getAbsolutePath());
		document.setFileSize(file.length());
	}

	public Document getDocument() {
		return document;
	}

	private void setUpInitialValues() throws ClassNotFoundException, IOException {
		setNumberOfDocuments();
		this.numberOfDocuments = initialValues.getNumberOfDocuments();
	}

	public void setNumberOfDocuments() throws ClassNotFoundException, IOException {
		initialValues.setNumberOfDocuments();
	}
}
